#ifndef _CANTHREAD_H
#define _CANTHREAD_H
#include <QObject>
#include <QThread>
#include "rtdb.h"
#include "Comstruct.h"
#include "KgGlobalData.h"
#include <QSettings>
#include <QDir>
#include "mainThread.h"

class CanThread: public QThread
{
    Q_OBJECT
public:
	CanThread(QObject *parent = 0, CRTDB *pRTDB = 0, int bcIndex = 0);
	~CanThread();
private:
	void run();	

	CRTDB *m_pRTDB;
    CBattStack *m_pBattStack;
    quint16 m_bmsDevCount;
    quint16 m_thDevCount;
    quint16 m_hkDevCount;
    QList<devData> m_bmsList;
    QList<devData> m_thList;
    QList<devData> m_hkList;

    void readBmsData(void);
    void funcBms3010(devData &dev, int index); //【科工】【BMS与PCS的通讯协议】     
    void funcBms3020(devData &dev, int index); //【团标】【中国化学与物理电源行业协会团体标准公告】
    void funcBms3100(devData &dev, int index); //【索英】【储能变流器与电池监控系统通信协议V1.0】
    void funcBms3101(devData &dev, int index); //【索英】【储能变流器与电池监控系统通信协议V1.6】
    void funcBms3150(devData &dev, int index); //【禾望】【PCS与BMS的CAN通信规约】
    void funcBms3160(devData &dev, int index); //【协能】【储能PCS与BMS-CAN通信协议】
    
    void readThData(void);
    void funcTh3010(devData &dev, CThData *pThData, int index); //【镐喆】【HZT04A温度采集板使用说明书】

    void readHkData(void);
    void funcHk3010(devData &dev, CEcData *pHkData, int index); //【奥威】【BP_ECU和BAMS通信协议V1.0】
    
    QString m_sComName;
    quint16 m_uComBaudRate;    
    
	bool m_bOpen;
    int  m_bcIndex;
    int ret;
    quint16 temp16;
    QDateTime m_ReInit;
    char a[8];

    char *m_pRecv;
    SCanMutiFrame *pCanMutiFrame;
    CXnCan m_can;
    int canInit(void);    
    void canSendFrame(quint32 id, int len, char *a, bool isExtand);

    int m_isOutputLog;    
 	KgLogFile m_logFile;   
    QString m_strLog;      
    void devConfig(void);
    void writeKgLogfile(QString strInfo,bool bSaveToFile=false);  
};

#define KG_MAX_LOG_SIZE     1024*1024*10
#define KG_LOG_INFO(MESSAGE)\
	do{ \
        QString strInfor;\
        QString currentFileName = QFileInfo(__FILE__).fileName();\
        QString currentFileLine = QString::number(__LINE__);\
        strInfor = currentFileName + "(" + currentFileLine + ") " + MESSAGE;\
        writeKgLogfile(strInfor,true);\
	} while(0)

#endif